import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/merchant/orders/OrderDetailPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';

class OrderWancheng extends StatefulWidget {
  OrderWancheng({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _OrderWancheng createState() => _OrderWancheng();
}

class _OrderWancheng extends State<OrderWancheng> {

  bool isShow=true;
  SharedPreferences prefs;
  String merchantID=""; //商户ID
  List orderList=[];    //已完成订单集合

  //初始化数据
  @override
  void initState() {
    super.initState();
    _getOrder();
  }

  // 加载商户已完成的订单
  _getOrder() async{
    prefs = await SharedPreferences.getInstance();
    merchantID = prefs.getString("merchantID");
    var res  =await HttpUtil().get("/waimai/mOder/getMerchantOrder?mId="+merchantID+"&orStatus=2");
    if(res['code']==200){
      orderList=res['data'];
      setState(() {

      });
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      body: ListView(
        children: [
          _getOrders(),
        ],
      )
    );
  }

  //遍历已完成订单
  _getOrders(){
    List<Widget> x = [];
    for(int i=0;i<orderList.length;i++){
      x.add(
        Container(
          padding: EdgeInsets.all(10),
          margin: EdgeInsets.only(bottom: 20),
          color: Colors.white,
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("订单号："+orderList[i]['id']),
                  Text(orderList[i]['orTime'].toString().substring(0,10))
                ],
              ),
              SizedBox(height: 10,),
              _getOrderDetail(orderList[i]['morderDetailList']),
              Container(
                child: Column(
                  children: [
                    Row(children: [
                      Expanded(child: orderList[i]['orRemark']!=null ? Text("备注："+orderList[i]['orRemark']) : Text(""),),
                      Text("共"+orderList[i]['orTotalnum'].toString()+"件"),
                      Text("合计:￥"+orderList[i]['orPrice'].toString())
                    ],),
                    SizedBox(height: 5,),
                    // Row(children: [
                    //   Icon(Icons.place_outlined,color: Colors.orange,),
                    //   SizedBox(width: 10,),
                    //   // Expanded(child: Text("湖南省株洲市天元区泰山街道珠江北路2000号幸福大厦909未来传媒")),
                    //   Expanded(child: Text(orderList[i]['address']['addressDetail'])),
                    // ],),
                    Row(children: [
                      GestureDetector(child: Icon(Icons.phone,color:Colors.blueAccent,),onTap: () async {
                        await launch("tel:"+orderList[i]['address']['adPhone']);
                      },),
                      SizedBox(width: 10,),
                      Expanded(child: Text("电话:"+orderList[i]['address']['adPhone']+" "+orderList[i]['address']['adName'])),
                      GestureDetector(
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 5,horizontal: 10),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.redAccent , width: 1) ,
                            borderRadius: BorderRadius.all(Radius.circular(6)) ,
                          ),
                          child: Text("详情"),
                        ),
                        onTap: (){
                          print("点击订单详情了");
                          MyRoute.push(OrderDetailPage(orId: orderList[i]['id'],));
                        },
                      )
                    ],),
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    }
    return Container(
      child: Column(children: x),
    );
  }

  //得到订单商品
  _getOrderDetail(orderDetailList){
    List<Widget> x = [];
    for(int i=0;i<orderDetailList.length;i++){
      x.add(
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            // Image.asset("assets/7.png",width: 90,height: 90,),
            Image.network(Config.getInstance().getHost()+orderDetailList[i]['mgoods']['gimages'],width: 90,height: 90,),
            SizedBox(width: 10,),
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(orderDetailList[i]['mgoods']['gname']),
                SizedBox(height: 5,),
                orderDetailList[i]['guiges']!=null ? Text(orderDetailList[i]['guiges'],style: TextStyle(color: Colors.black38),) : Text(""),
                // Text("*"+orderDetailList[i]['odNum'].toString())
              ],
            ),),
            Text("￥"+orderDetailList[i]['odCost'].toString()),
          ],
        ),
      );
    }
    return Container(
      child: Column(children: x),
    );
  }

}